package com.wing.qualityTesting.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.qualityTesting.dao.ProcessQualityDao;
import com.wing.qualityTesting.model.entity.ProcessQuality;
import com.wing.qualityTesting.model.request.ProcessQualityForm;
import com.wing.qualityTesting.model.request.ProcessQualityValueForm;
import com.wing.qualityTesting.model.response.ProcessQualityVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 质量参数-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProcessQualityServiceImpl extends ServiceImpl<ProcessQualityDao, ProcessQuality> implements ProcessQualityService {

    @Autowired
    private ProcessQualityValueService processQualityValueService;
    @Override
    public int add(ProcessQualityForm processQualityForm) {

        //校验

        ProcessQuality processQuality =new ProcessQuality();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProcessQuality.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(processQualityForm, processQuality, copyOptions);
        int insert = baseMapper.insert(processQuality);
        if(insert>0&&processQuality.getType().equals(2)){
            // 新增value
            for (String value : processQualityForm.getProcessQualityValue()) {
                ProcessQualityValueForm processQualityValueForm = new ProcessQualityValueForm();
                processQualityValueForm.setProcessQualityValue(value);
                processQualityValueForm.setProcessQualityId(processQuality.getId());
                processQualityValueService.add(processQualityValueForm);
            }

        }
        return insert;
    }

    @Override
    public List<ProcessQualityVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProcessQualityVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProcessQualityVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProcessQualityVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
